package com.gitee.hljdrl.dsbridge.util;

import android.annotation.SuppressLint;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.ffsky.logger.L;

public class DSSettingUtil {

    private static String USER_AGENT = "";

    public static void setUserAgent(String userAgent) {
        USER_AGENT = userAgent;
    }

    @SuppressLint("SetJavaScriptEnabled")
    public static void settings(WebView webView) {
        if (webView == null) {
            return;
        }
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        //-----------------------------------------------
        webView.setFadingEdgeLength(0);

        webView.setHorizontalFadingEdgeEnabled(false);
        webView.setVerticalFadingEdgeEnabled(false);
        //
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            settings.setMediaPlaybackRequiresUserGesture(false);
        }
        settings.setLoadWithOverviewMode(true);
        webView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        settings.setDomStorageEnabled(true);    //开启DOM形式存储
        settings.setDatabaseEnabled(true);   //开启数据库形式存储
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(false);

        settings.setTextZoom(100);

        settings.setUseWideViewPort(true);
        settings.setLoadsImagesAutomatically(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        }
        String ua = settings.getUserAgentString();
        settings.setUserAgentString(setWebViewUserAgent(ua));
    }

    @SuppressLint("SetJavaScriptEnabled")
    public static void settings(WebView webView, boolean enableZoom) {
        if (webView == null) {
            return;
        }
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        //-----------------------------------------------
        webView.setFadingEdgeLength(0);
        webView.setHorizontalFadingEdgeEnabled(false);
        webView.setVerticalFadingEdgeEnabled(false);
        //
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        //
        //
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            settings.setMediaPlaybackRequiresUserGesture(false);
        }
        settings.setLoadWithOverviewMode(true);
        //
        webView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        settings.setDomStorageEnabled(true);    //开启DOM形式存储
        settings.setDatabaseEnabled(true);   //开启数据库形式存储
        settings.setSupportZoom(enableZoom);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(enableZoom);

        settings.setTextZoom(100);

        ////将图片调整到适合webview的大小
        settings.setUseWideViewPort(true);
        settings.setLoadsImagesAutomatically(true); //支持自动加载图片
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        }
        //-----------------------------------------------
        String ua = settings.getUserAgentString();
        // 设置浏览器UA,JS端通过UA判断是否属于Quick环境
        settings.setUserAgentString(setWebViewUserAgent(ua));
    }

    private static String setWebViewUserAgent(String _ua) {
        if (TextUtils.isEmpty(USER_AGENT)) {
            return _ua;
        }
        StringBuffer buf = new StringBuffer();
        buf.append(_ua);
        buf.append(" ");
        buf.append(USER_AGENT);
        L.i("WebSettingsCompat", "setUserAgent-->", buf.toString());
        return buf.toString();
    }

    public static void clearUserCookie() {
        try {
            CookieManager cookieManager = CookieManager.getInstance();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                cookieManager.removeSessionCookies(null);
                cookieManager.removeAllCookies(null);
                cookieManager.flush();
            }
        } catch (NoSuchMethodError | Exception error) {
            error.printStackTrace();
        }
    }


    public void clearCache(WebView webView) {
        try{
            // 清除缓存
            webView.clearCache(true);
            // 清除数据库
//            webView.clearDatabase();
            // 清除LocalStorage
//            webView.loadUrl("javascript:localStorage.clear()");
            // 清除IndexedDB
//            webView.loadUrl("javascript:indexedDB.databases().then((r) => { for(var i = 0; i < r.length; i++) indexedDB.deleteDatabase(r[i].name); })");
            // 清除Cookies
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
            // 清除表单数据
            webView.clearFormData();
            // 清除历史记录
            webView.clearHistory();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

}
